#include "LoginWindow.h"
#include <QMessageBox>

LoginWindow::LoginWindow(QWidget *parent) : QWidget(parent) {
    // 窗口基础设置
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    resize(800, 600);

    // 1. 设置动态GIF背景
    setupGifBackground();

    // 2. 创建页面容器
    stackedWidget = new QStackedWidget(this);
    stackedWidget->setFixedSize(400, 300);
    stackedWidget->move((width() - 400) / 2, (height() - 300) / 2); // 居中

    // 3. 初始化登录页和注册页
    setupLoginPage();
    setupRegisterPage();

    // 4. 添加半透明遮罩（提升文字可读性）
    QWidget *mask = new QWidget(stackedWidget);
    mask->setStyleSheet("background-color: rgba(0,0,0,0.4); border-radius: 15px;");
    mask->setGeometry(0, 0, 400, 300);
}

// 初始化GIF背景
void LoginWindow::setupGifBackground() {
    movie = new QMovie(":/bg.gif");
    bgLabel = new QLabel(this);
    bgLabel->setMovie(movie);
    movie->start();
    bgLabel->setScaledContents(true);
    bgLabel->resize(size());
    bgLabel->lower(); // 置于底层
}

// 创建登录页控件
void LoginWindow::setupLoginPage() {
    QWidget *loginPage = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(loginPage);

    // 用户名输入框
    loginUserEdit = new QLineEdit;
    loginUserEdit->setPlaceholderText("Username");
    loginUserEdit->setStyleSheet("background: rgba(255,255,255,0.8); padding: 10px; border-radius: 5px;");
    layout->addWidget(loginUserEdit);

    // 密码输入框
    loginPassEdit = new QLineEdit;
    loginPassEdit->setPlaceholderText("Password");
    loginPassEdit->setEchoMode(QLineEdit::Password);
    loginPassEdit->setStyleSheet(loginUserEdit->styleSheet());
    layout->addWidget(loginPassEdit);

    // 登录按钮
    loginButton = new QPushButton("Login");
    loginButton->setStyleSheet("background: #4CAF50; color: white; padding: 12px; border-radius: 5px;");
    connect(loginButton, &QPushButton::clicked, this, &LoginWindow::handleLogin);
    layout->addWidget(loginButton);

    // 跳转注册按钮
    toRegisterButton = new QPushButton("Create Account");
    toRegisterButton->setStyleSheet("color: #2196F3; background: transparent;");
    connect(toRegisterButton, &QPushButton::clicked, this, &LoginWindow::switchToRegister);
    layout->addWidget(toRegisterButton);

    stackedWidget->addWidget(loginPage);
}

// 创建注册页控件
void LoginWindow::setupRegisterPage() {
    QWidget *registerPage = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout(registerPage);

    regUserEdit = new QLineEdit;
    regUserEdit->setPlaceholderText("New Username");
    regUserEdit->setStyleSheet(loginUserEdit->styleSheet());
    layout->addWidget(regUserEdit);

    regPassEdit = new QLineEdit;
    regPassEdit->setPlaceholderText("Password");
    regPassEdit->setEchoMode(QLineEdit::Password);
    regPassEdit->setStyleSheet(loginUserEdit->styleSheet());
    layout->addWidget(regPassEdit);

    regConfirmPassEdit = new QLineEdit;
    regConfirmPassEdit->setPlaceholderText("Confirm Password");
    regConfirmPassEdit->setEchoMode(QLineEdit::Password);
    regConfirmPassEdit->setStyleSheet(loginUserEdit->styleSheet());
    layout->addWidget(regConfirmPassEdit);

    registerButton = new QPushButton("Register");
    registerButton->setStyleSheet(loginButton->styleSheet());
    connect(registerButton, &QPushButton::clicked, this, &LoginWindow::handleRegister);
    layout->addWidget(registerButton);

    toLoginButton = new QPushButton("Back to Login");
    toLoginButton->setStyleSheet(toRegisterButton->styleSheet());
    connect(toLoginButton, &QPushButton::clicked, this, &LoginWindow::switchToLogin);
    layout->addWidget(toLoginButton);

    stackedWidget->addWidget(registerPage);
}

// --- 功能槽函数实现 ---
void LoginWindow::switchToRegister() { stackedWidget->setCurrentIndex(1); }
void LoginWindow::switchToLogin()    { stackedWidget->setCurrentIndex(0); }

void LoginWindow::handleLogin() {
    QString user = loginUserEdit->text();
    QString pass = loginPassEdit->text();
    if (user.isEmpty() || pass.isEmpty()) {
        QMessageBox::warning(this, "Error", "Username and password cannot be empty!");
    } else {
        // 实际开发中此处连接数据库验证
        QMessageBox::information(this, "Success", "Login successful!");
    }
}

void LoginWindow::handleRegister() {
    if (regPassEdit->text() != regConfirmPassEdit->text()) {
        QMessageBox::warning(this, "Error", "Passwords do not match!");
        return;
    }
    // 实际开发中此处写入数据库
    QMessageBox::information(this, "Success", "Registration complete!");
    switchToLogin(); // 注册成功后跳回登录页
}
